$(function(){

    //报表类型
    var type,year,month,date,textName,fuText;
    var search = function ()
    {
    	type = $('#type').val();
        year = $('#year').val();
        month = $('#month').val();
    	date = year + "-"+month;
    	$('.reportMonth').show();
    	url = "/modules/operatingStatistics/borrowProductDistributed.html?type="+type+"&date="+date + "&randomTime=" + (new Date()).getTime();
    	ajaxUrl(url,type);
    }

	var ajaxUrl = function (url,type){
		$.ajax({
			url:url,
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				var sums = 0;
				for (var i=0;i<data.distributed.length;i++) 
				{
					sums = sums+data.distributed[i];
					if(sums == 0)
					{
						fuText = '暂无数据';
					}
					else
					{
						fuText = '';
					}
					if(data.distributed[i] == 0)
					{
						delete data.distributed[i];
					}
				};
				var option;
				if(type == 0)//type0
				{
					option = {
						title : {
						    text: '借款产品笔数分布及占比',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						calculable : true,
						legend: {
					        orient : 'vertical',
					        x : '70%',
					        y : 80,
					        data:data.typeNames
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						series : [
						    {
						        name:'笔数分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
						            {value:data.distributed[0],name:data.typeNames[0]},
						            {value:data.distributed[1],name:data.typeNames[1]},
						            {value:data.distributed[2],name:data.typeNames[2]},
						            {value:data.distributed[3],name:data.typeNames[3]},
						        ]
						    }
						]
					}
				}
				else if(type == 1)//type1
				{
					option = {
						title : {
						    text: '借款产品金额分布及占比',
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
						legend: {
					        orient : 'vertical',
					        x : '70%',
					        y : 80,
					        data:data.typeNames
					    },
						tooltip : {
						    show: true,
						    formatter: "{a} <br/>{b} : {c} ({d}%)"
						},
						calculable : true,
						series : [
						    {
						        name:'金额分布',
						        type:'pie',
						        radius : '50%',
						        center: ['50%', '65%'],
						        data:[
									{value:data.distributed[0],name:data.typeNames[0]},
									{value:data.distributed[1],name:data.typeNames[1]},
									{value:data.distributed[2],name:data.typeNames[2]},
									{value:data.distributed[3],name:data.typeNames[3]},
						        ]
						    }
						]
					}
				}
				// 基于准备好的dom，初始化echarts图表
		        var myChart = echarts.init(document.getElementById('investTablea')); 
		        // 为echarts对象加载数据 
		        myChart.setOption(option); 
			}
		});
	};

    var changBtn1 = $('.investorsTabs .tabs-header ul li a:eq(0)');
	changBtn1.click(function(){
		search();//初始化加载
	});
    $("#type,#year,#month").change(function(){
		search();
    });
    search();


	//按钮二
	//报表类型

    var searchb = function ()
    {
    	borrowType = $('#borrowType').val();
    	type = $('#basicType').val();
        year = $('#basicYear').val();
        month = $('#basicMonth').val();
    	date = year + "-"+month;
    	textName = $("#basicType[name=basicType]").find("option:selected").text();
   		$('.biaoType').show();
    	$('.reportMonth').hide();
    	url = "/modules/operatingStatistics/borrowInfoAnalysis.html?type="+type+"&year="+year+"&borrowType="+borrowType + "&randomTime=" + (new Date()).getTime();
    	ajaxUrl2(url,type,textName);
	    
    }
    
	var ajaxUrl2 = function (url,type,textName){
		$.ajax({
			url:url,
			type:"get",
			dataType:"json",
			cache:false,
			success:function(data){
				var sums = 0;
				for (var i=0;i<data.counts.length;i++) 
				{
					sums = sums+data.counts[i];
					if(sums == 0)
					{
						fuText = '暂无数据';
					}
					else
					{
						fuText = '';
					}
				};
				var option;
				if(type == 1)
				{
					option = {
						title : {
						    text: textName,
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	x:'center',
					    	y:'bottom',
					        data:['成交笔数','同比值']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            data : data.dates
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            name : '成交笔数',
					            axisLabel : {
					                formatter: '{value} 笔'
					            }
					        },
					        {
					            type : 'value',
					            name : '同比值',
					            axisLabel : {
					                formatter: '{value} %'
					            }
					        }
					    ],
					    series : [

					        {
					            name:'成交笔数',
					            type:'bar',
					            data: data.counts
					        },
					        {
					            name:'同比值',
					            type:'line',
					            yAxisIndex: 1,
					            data: data.ratios
					        }
					    ]
					};    
				}
				else if(type == 2)
				{
					option = {
						title : {
						    text: textName,
						    x:'center',
			            	y:'top',
			            	padding:30,
			            	textStyle:{
									fontSize: 24, 
									fontWeight: 'bolder'	
			            	},
			            	subtext:fuText
						},
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					    	x:'center',
					    	y:'bottom',
					        data:['成交金额','同比值']
					    },
					    xAxis : [
					        {
					            type : 'category',
					            data : data.dates
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            name : '成交金额',
					            axisLabel : {
					                formatter: '{value} 元'
					            }
					        },
					        {
					            type : 'value',
					            name : '同比值',
					            axisLabel : {
					                formatter: '{value} %'
					            }
					        }
					    ],
					    series : [

					        {
					            name:'成交金额',
					            type:'bar',
					            data: data.counts
					        },
					        {
					            name:'同比值',
					            type:'line',
					            yAxisIndex: 1,
					            data: data.ratios
					        }
					    ]
					};    
				}
				
				// 基于准备好的dom，初始化echarts图表
		        var myChart = echarts.init(document.getElementById('investTableb')); 
		        // 为echarts对象加载数据 
		        myChart.setOption(option); 
			}
		});
	};
	var changBtn2 = $('.investorsTabs .tabs-header ul li a:eq(1)');
	changBtn2.click(function(){
		searchb();//初始化加载
	});
    $("#basicType,#basicYear,#basicMonth,#borrowType").change(function(){
    	searchb();
    });
});